import React from 'react'
import { Text, View } from 'react-native'
import { Metrics, Colors, Fonts } from '../../Themes'
import { API, WINDOW } from '../../Services'
import styles from '../Styles/CommonContainerStyles'
import { WebView } from 'react-native-webview';

class WebViewScreen extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            ...this.props.route.params,
            type: this.props.route.params.type ? this.props.route.params.type : 0,   //0文章详情 1使用协议 2隐私协议  3首页banner
        }
        if (this.state.type === 1 || this.state.type === 2) {
            this.props.navigation.addListener('blur', () => {
                this.props.route.params.callBack && this.props.route.params.callBack()
            })
        }
    }

    componentDidMount() {
        if (this.state.type === 1) {
            this.props.navigation.setOptions({ title: '《用户服务协议》' })
            API.getProtocol_sy().then((response) => {
                const { ok, data } = response
                if (ok) {
                    this.setState({ content: data })
                }
            })
        }
        else if (this.state.type === 2) {
            this.props.navigation.setOptions({ title: '《隐私政策》' })
            API.getProtocol_ys().then((response) => {
                const { ok, data } = response
                if (ok) {
                    this.setState({ content: data })
                }
            })
        }else if (this.state.type === 0) {
            this.props.navigation.setOptions({ title: this.state.title })
        }else if (this.state.type === 3) {

        }
    }

    render() {
        if (this.state.type === 0) {
            let url = WINDOW.baseURL + 'open/news/newsInfo/id/' + this.state.id + '.html'
            console.log(url)
            return (
                <View style={styles.container}>
                    <WebView
                        style={{ backgroundColor: 'transparent' }}
                        source={{ uri: url }}
                        originWhitelist={['*']}
                        javaScriptEnabled={true}
                    />
                </View>
            )
        }
        else if (this.state.type === 3) {
            console.log(this.state.to_url)
            return (
                <View style={styles.container}>
                    <WebView
                        style={{ backgroundColor: 'transparent' }}
                        source={{ uri: this.state.to_url }}
                        originWhitelist={['*']}
                        javaScriptEnabled={true}
                    />
                </View>
            )
        } else {
            return (
                <View style={styles.container}>
                    <WebView
                        style={{ backgroundColor: 'transparent' }}
                        source={{ html: this.state.content }}
                        originWhitelist={['*']}
                        javaScriptEnabled={true}
                    />
                </View>
            )
        }
    }
}


export default WebViewScreen
